官方文档: https://matplotlib.org/
Matplotlib中最基础的模块是pyplot
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
# 添加画布 figsize=(7,4)表示画布比例
plt.figure(figsize=(7,4))
# 编码错误、中文显示
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
# 绘制图像,默认为线图
# x表示x x**2表示y ls表示线的样式
plt.plot(x,x**2,ls='-.')
plt.plot(x,x**4,ls=':')
# 添加图例
plt.title('line-p')
# 添加图例
plt.legend(['y=x^2','y=x^4'])
# 添加x、y轴标题
plt.xlabel('x')
plt.ylabel('x^2/x^4')
# 保存图像
plt.savefig('line-p.png')
# 展示图像
plt.show()
data = np.load('国民经济核算季度数据.npz',allow_pickle=True)
data.files
['columns', 'values']
columns = data['columns']
values = data['values']
columns,values
(array(['序号', '时间', '国内生产总值_当季值(亿元)', '第一产业增加值_当季值(亿元)', '第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)', '农林牧渔业增加值_当季值(亿元)', '工业增加值_当季值(亿元)',
'建筑业增加值_当季值(亿元)', '批发和零售业增加值_当季值(亿元)', '交通运输、仓储和邮政业增加值_当季值(亿元)',
'住宿和餐饮业增加值_当季值(亿元)', '金融业增加值_当季值(亿元)', '房地产业增加值_当季值(亿元)',
'其他行业增加值_当季值(亿元)'], dtype=object),
array([[1, '2000年第一季度', 21329.9, ..., 1235.9, 933.7, 3586.1],
[2, '2000年第二季度', 24043.4, ..., 1124.0, 904.7, 3464.9],
[3, '2000年第三季度', 25712.5, ..., 1170.4, 1070.9, 3518.2],
...,
[67, '2016年第三季度', 190529.5, ..., 15472.5, 12164.1, 37964.1],
[68, '2016年第四季度', 211281.3, ..., 15548.7, 13214.9, 39848.4],
[69, '2017年第一季度', 180682.7, ..., 17213.5, 12393.4, 42443.1]],
dtype=object))
plt.figure(figsize=(9,4))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.scatter(values[:,1],values[:,3],marker='.',c='m')
plt.scatter(values[:,1],values[:,4],marker='.',c='c')
plt.scatter(values[:,1],values[:,5],marker='.',c='y')
plt.title('2000-2016GDP')
plt.xlabel('The Time')
plt.ylabel('The GDP')
plt.legend(['第一产业增加值_当季值(亿元)',
'第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)'])
plt.xticks(range(0,70,5),rotation=60)
plt.savefig('2000-2016GDP_scatter.png')
plt.show()
columns,values
(array(['序号', '时间', '国内生产总值_当季值(亿元)', '第一产业增加值_当季值(亿元)', '第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)', '农林牧渔业增加值_当季值(亿元)', '工业增加值_当季值(亿元)',
'建筑业增加值_当季值(亿元)', '批发和零售业增加值_当季值(亿元)', '交通运输、仓储和邮政业增加值_当季值(亿元)',
'住宿和餐饮业增加值_当季值(亿元)', '金融业增加值_当季值(亿元)', '房地产业增加值_当季值(亿元)',
'其他行业增加值_当季值(亿元)'], dtype=object),
array([[1, '2000年第一季度', 21329.9, ..., 1235.9, 933.7, 3586.1],
[2, '2000年第二季度', 24043.4, ..., 1124.0, 904.7, 3464.9],
[3, '2000年第三季度', 25712.5, ..., 1170.4, 1070.9, 3518.2],
...,
[67, '2016年第三季度', 190529.5, ..., 15472.5, 12164.1, 37964.1],
[68, '2016年第四季度', 211281.3, ..., 15548.7, 13214.9, 39848.4],
[69, '2017年第一季度', 180682.7, ..., 17213.5, 12393.4, 42443.1]],
dtype=object))
plt.figure(figsize=(8,5))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.plot(values[:,1],values[:,3],ls='-.',c='m')
plt.plot(values[:,1],values[:,4],ls='--',c='c')
plt.plot(values[:,1],values[:,5],ls=':',c='b')
plt.title('2000-2016GDP')
plt.xlabel('The GDP')
plt.ylabel('The Time')
plt.legend(['第一产业增加值_当季值(亿元)',
'第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)'])
plt.xticks(range(0,70,5),rotation=45)
plt.savefig('2000-2016GDP_plot.png')
plt.show()
columns,values
(array(['序号', '时间', '国内生产总值_当季值(亿元)', '第一产业增加值_当季值(亿元)', '第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)', '农林牧渔业增加值_当季值(亿元)', '工业增加值_当季值(亿元)',
'建筑业增加值_当季值(亿元)', '批发和零售业增加值_当季值(亿元)', '交通运输、仓储和邮政业增加值_当季值(亿元)',
'住宿和餐饮业增加值_当季值(亿元)', '金融业增加值_当季值(亿元)', '房地产业增加值_当季值(亿元)',
'其他行业增加值_当季值(亿元)'], dtype=object),
array([[1, '2000年第一季度', 21329.9, ..., 1235.9, 933.7, 3586.1],
[2, '2000年第二季度', 24043.4, ..., 1124.0, 904.7, 3464.9],
[3, '2000年第三季度', 25712.5, ..., 1170.4, 1070.9, 3518.2],
...,
[67, '2016年第三季度', 190529.5, ..., 15472.5, 12164.1, 37964.1],
[68, '2016年第四季度', 211281.3, ..., 15548.7, 13214.9, 39848.4],
[69, '2017年第一季度', 180682.7, ..., 17213.5, 12393.4, 42443.1]],
dtype=object))
plt.figure(figsize=(8,5))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
# plt.pie?
# values[1,3:6]:值
# labels=labels:每个值的标签
# explode=[0.03,0.04,0.03]:每块区域的分离度
# autopct='%1.1f%%':数据标签显示
labels=['第一产业增加值_当季值(亿元)',
'第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)']
plt.pie(values[0,3:6],explode=[0.03,0.03,0.03],
labels=labels,autopct='%1.1f%%')
plt.title('2000GDP')
plt.savefig('2000GDP.png')
plt.show()
columns,values
(array(['序号', '时间', '国内生产总值_当季值(亿元)', '第一产业增加值_当季值(亿元)', '第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)', '农林牧渔业增加值_当季值(亿元)', '工业增加值_当季值(亿元)',
'建筑业增加值_当季值(亿元)', '批发和零售业增加值_当季值(亿元)', '交通运输、仓储和邮政业增加值_当季值(亿元)',
'住宿和餐饮业增加值_当季值(亿元)', '金融业增加值_当季值(亿元)', '房地产业增加值_当季值(亿元)',
'其他行业增加值_当季值(亿元)'], dtype=object),
array([[1, '2000年第一季度', 21329.9, ..., 1235.9, 933.7, 3586.1],
[2, '2000年第二季度', 24043.4, ..., 1124.0, 904.7, 3464.9],
[3, '2000年第三季度', 25712.5, ..., 1170.4, 1070.9, 3518.2],
...,
[67, '2016年第三季度', 190529.5, ..., 15472.5, 12164.1, 37964.1],
[68, '2016年第四季度', 211281.3, ..., 15548.7, 13214.9, 39848.4],
[69, '2017年第一季度', 180682.7, ..., 17213.5, 12393.4, 42443.1]],
dtype=object))
plt.figure(figsize=(8,5))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
label=labels=['第一产业增加值_当季值(亿元)',
'第二产业增加值_当季值(亿元)',
'第三产业增加值_当季值(亿元)']
# values[:,3:6]:值
# notch=True:中位数凹陷
# labels=labels:数据标签
plt.boxplot(values[:,3:6],notch=True,labels=label,
meanline=False)
plt.xlabel('The Time')
plt.ylabel('The GDP')
plt.title('2000-2017GDP')
plt.savefig('2000-2017GDP_boxplot.png')
plt.show()
plt.figure(figsize=(8,5))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.title('2000-2017GDP')
plt.xlabel('The Time')
plt.ylabel('The GDP')
plt.boxplot(values[:,3],labels=['第一产业增加值_当季值(亿元)'])
plt.savefig('2000-2017GDP_boxplot_single.png')
plt.show()